package com.zjut.zs.user.action;

import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zjut.an.acquisitionhistory.dao.AnAcquisitionHistory;
import com.zjut.an.acquisitionhistory.manager.AnAcquisitionHistoryManager;
import com.zjut.an.config.dao.AnConfig;
import com.zjut.an.config.manager.AnConfigManager;
import com.zjut.an.log.dao.AnLog;
import com.zjut.an.log.manager.AnLogManager;
import com.zjut.an.user.dao.AnUser;
import com.zjut.an.util.email.SendsimpleMail;
import com.zjut.an.util.module.PageModule;
import com.zjut.an.util.system.Config;
import com.zjut.an.util.system.SMSHelper;
import com.zjut.an.util.system.Status;
import com.zjut.an.util.tools.DateUtil;
import com.zjut.an.util.tools.JSONUtilClass;
import com.zjut.an.util.tools.JSONUtilClassTime;
import com.zjut.an.util.tools.MD5;
import com.zjut.an.util.tools.StringUtil;
import com.zjut.common.idgen.IdGenerate;
import com.zjut.zs.applyview.dao.ZsApplyView;
import com.zjut.zs.applyview.manager.ZsApplyViewManager;
import com.zjut.zs.school.dao.ZsSchool;
import com.zjut.zs.school.manager.ZsSchoolManager;
import com.zjut.zs.user.dao.ZsUser;
import com.zjut.zs.user.manager.ZsUserManager;
import com.zjut.zs.userstatus.dao.ZsUserStatus;
import com.zjut.zs.userstatus.manager.ZsUserStatusManager;

public class ZsUserAction extends ActionSupport implements
ServletResponseAware, ServletRequestAware, ModelDriven<ZsUser>{

	private HttpServletRequest request;
	private HttpServletResponse response;
	private HttpSession session;
	
	private ZsUser obj;
	private Map<String , Object> dataMap;
	private int page = 1, limit = 10;
	private String order;
	private String sort;
	private String search;
	private String rowstr;
	
	@Resource(name = "ZsUserManager")
	private ZsUserManager manager;

	@Resource(name = "ZsUserStatusManager")
	private ZsUserStatusManager smanager;
	
	@Resource(name = "AnLogManager")
	private AnLogManager logManager;

	@Resource(name = "AnConfigManager")
	private AnConfigManager anConfigManager;

	@Resource(name = "ZsSchoolManager")
	private ZsSchoolManager scmanager;

	@Resource(name = "AnAcquisitionHistoryManager")
	private AnAcquisitionHistoryManager acmanager;
	
	@Resource(name = "ZsApplyViewManager")
	private ZsApplyViewManager vmanager;

	@Resource(name = "IdGenerate")
	private IdGenerate idgenerate;
	
	@Resource(name = "StringUtil")
	private StringUtil stringutil;
	
	@Resource(name = "MD5")
	private MD5 md5;

	@Resource(name = "DateUtil")
	private DateUtil dateutil;
	
	public String zsUserList() {
		try {
			request.setCharacterEncoding("UTF-8");
			dataMap = new HashMap<String, Object>();			
			if (session.getAttribute("admin")!=null) {//只有管理员才允许执行该操作，以免信息泄露
				if(search!=null) {
					obj = new ZsUser();
					obj.setUsName(search);
					search = null;
				}
				PageModule module = manager.getDataByInstance(page, limit, sort, order, obj);
				dataMap.put("rows", module.getObjects());
				dataMap.put("total", module.getTotal()); 
			}			
			obj = null; 
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}	
	}

	public String zsUserCount() {//用于外部用户注册时候进行账号验证，以避免使用list泄露信息
		try {
			request.setCharacterEncoding("UTF-8");
			dataMap = new HashMap<String, Object>();
			System.out.println("#########" + search + " idcard" + obj.getUsIdcard()+ " mobile"+obj.getUsMobile());			
			if(obj.getUsIdcard()!=null&&obj.getUsIdcard().trim().length()==0) {
				dataMap.put("total", 0); 				
			} else {
				PageModule module = manager.getDataByInstance(page, limit, sort, order, obj);
				//dataMap.put("rows", module.getObjects());
				dataMap.put("total", module.getTotal()); 
				System.out.println("######### total " + module.getTotal());		
				if (module.getTotal()>0) {
					obj = (ZsUser)module.getObjects().get(0);
					dataMap.put("usactive", obj.getUsActive()); 
				}
				obj = null; 
			}			
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}		
	}
	
	@SuppressWarnings("unchecked")
	public String zsUserDeletes() {
		try {
			String objumniJSON = rowstr;
			if (session.getAttribute("admin")!=null) {//只有管理员才允许执行该操作，以免信息泄露
				manager.deleteDatas(JSONUtilClassTime.getDTOList(objumniJSON, ZsUser.class));
			}		
			dataMap = new HashMap<String, Object>();
			dataMap.put("flag", "success");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}		
	}
	
	public String zsUserAdd() {
		dataMap = new HashMap<String, Object>();
		try {
			request.setCharacterEncoding("UTF-8");
			String objumniJSON = rowstr;
			obj = (ZsUser)JSONUtilClass.getDTO(objumniJSON, ZsUser.class);
			if(Status.ON.getStatus().equals(((ZsSchool)scmanager.getDatas().get(0)).getScRegActive())){ //如果注册仍然开启
				ZsUser obaccount = new ZsUser();
				obaccount.setUsIdcard(obj.getUsIdcard());
				if (!JSONUtilClass.getMapFromJson(objumniJSON).get("code").equals(session.getAttribute("code"))) {
					dataMap.put("flag", "success");
					dataMap.put("message", "验证码错误");
				} if(manager.getDataByInstance(1, 1, null, null, obaccount).getObjects().size()>0) {
					dataMap.put("flag", "success");
					dataMap.put("message", "该身份证账号已经注册过");
				} else {
					obj.setUsId(idgenerate.nextId());
					//AnConfig config = anConfigManager.getConfigInfoByName("报名序号");//获取当前的报名序号
					AnAcquisitionHistory anac = new AnAcquisitionHistory();
					anac.setAcId(obj.getUsId());
					anac.setChannelUrl("报名序号编号");
					anac.setDescription("注册");
					anac.setContentUrl(obj.getUsIdcard());
					anac.setNeId(idgenerate.randomInt(100000000, 200000000));//写入id，使得neid唯一，确保抽签不重复写入
					acmanager.saveHistory(anac);
					int place = acmanager.getDataPlaceById(anac);					
					obj.setUsNo(anConfigManager.getSingleConfigInfoByName("报名号前缀") + smanager.getStringNo(place));

					obj.setUsPassword(md5.toMD5(obj.getUsPassword()));
					obj.setAddTime(dateutil.getCurrentDate5());
					obj.setEditTime(dateutil.getCurrentDate5());
					obj.setLoginTime(dateutil.getCurrentDate5());
					obj.setUsActive(Status.USER_ACTIVE_ON.getStatus());
					manager.addData(obj);
					
					ZsSchool school = (ZsSchool)scmanager.getDatas().get(0);
					
					ZsUserStatus status = new ZsUserStatus();
					status.setUssId(idgenerate.nextId());
					status.setZsUser(obj);
					status.setScId(school.getScId());
					status.setScCode(school.getScCode());
					status.setUsIdcard(obj.getUsIdcard());
					status.setYear(Integer.parseInt(dateutil.getCurrentDate().substring(0, 3)));
					status.setAppStatus(Status.APPSTATUS_NULL.getStatus());
					status.setAppTime(dateutil.getCurrentDate5());
					status.setSendStatus(Status.SENDSTATUS_NULL.getStatus());
					status.setFileStatus(Status.FILESTATUS_NULL.getStatus());
					status.setCheckStatus(Status.CHECKSTATUS_NULL.getStatus());
					status.setEmsStatus(Status.EMSSTATUS_NULL.getStatus());
					status.setPayStatus(Status.PAYSTATUS_NULL.getStatus());
					status.setPassStatus(Status.PASSSTATUS_NULL.getStatus());
					status.setLocalStatus(Status.LOCALSTATUS_NO.getStatus());			
					smanager.addData(status);			
					
					AnLog log = new AnLog(obj.getUsId(),"考生日志",obj.getUsName()+"("+logManager.subIdcard(obj.getUsIdcard())+")"+"于"+dateutil.getCurrentDate5()+" 注册成功", dateutil.getCurrentDate5());
					logManager.addData(log);
					
					dataMap.put("flag", "success");
					dataMap.put("message", "注册成功");
					dataMap.put("user", obj);
				}
				return SUCCESS;
			} else {
				dataMap.put("message", "停止注册");//否则禁止注册
				return ERROR;
			}			
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	
	public String zsUserDelete() {
		try {
			String objumniJSON = rowstr;
			if (session.getAttribute("admin")!=null) {//只有管理员才允许执行该操作，以免信息泄露
				manager.deleteData((ZsUser)JSONUtilClassTime.getDTO(objumniJSON, ZsUser.class));
			}
			dataMap = new HashMap<String, Object>();
			dataMap.put("flag", "success");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}		
	}
	
	
	public String zsUserUpdate() {
		try {
			String objumniJSON = rowstr;
			obj = (ZsUser)JSONUtilClassTime.getDTO(objumniJSON, ZsUser.class);
			
			ZsUser zs = manager.getData(obj.getUsId());
			if (null!=obj.getUsPassword() && obj.getUsPassword().trim().length()>0 && !obj.getUsPassword().equals(zs.getUsPassword())) {
				obj.setUsPassword(md5.toMD5(obj.getUsPassword()));
			} else if(null==obj.getUsPassword() || obj.getUsPassword().trim().length()==0) {
				obj.setUsPassword(zs.getUsPassword());
			}
			obj.setEditTime(dateutil.getCurrentDate5());
			session.setAttribute("zsUser", obj);
			manager.updateData(obj);
			AnLog log = new AnLog(obj.getUsId(),"考生日志",obj.getUsName()+"("+logManager.subIdcard(obj.getUsIdcard())+")"+"于"+dateutil.getCurrentDate5()+" 修改个人信息", dateutil.getCurrentDate5());
			logManager.addData(log);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}		
	}
	
	public String zsUserSave() {
		obj = (ZsUser)(JSONUtilClass.getDTOArray(rowstr, ZsUser.class)[0]);
		if (!stringutil.isEmpty(obj.getUsId())) {
			//-------update-------
			manager.updateData(obj);
		} else {
			//-------insert-------
			obj.setUsId(idgenerate.nextId());
			manager.addData(obj);
		}
		rowstr = null;
		return SUCCESS;
	}

	public String zsUser() {
		try {
			request.setCharacterEncoding("UTF-8");
			dataMap = new HashMap<String, Object>();
			String inId = request.getParameter("inId");
			obj = manager.getData(inId);
			dataMap.put("rows", obj);
			obj = null;
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}		
	}

	public String zsUserLogin() {
		System.out.println("-------UserLogin-------");
		try {
			dataMap = new HashMap<String, Object>();
			String adminJSON = rowstr;
			//System.out.println("rand---"+JSONUtilClass.getMapFromJson(adminJSON).get("rand"));
			//System.out.println(adminJSON);			
			
			if(Status.ON.getStatus().equals(((ZsSchool)scmanager.getDatas().get(0)).getScLoginActive())){//如果登录接口开启

				obj = (ZsUser)JSONUtilClass.getDTO(adminJSON, ZsUser.class);
				String password = obj.getUsPassword(); 
				if (!JSONUtilClass.getMapFromJson(adminJSON).get("rand").equals(session.getAttribute("rand"))) {
					dataMap.put("message", "randfailed");
				} else if (null==obj.getUsPassword() || obj.getUsPassword().trim().length()==0) {
					dataMap.put("message", "failed");
				} else {
					obj.setUsPassword(null);
					PageModule pm = manager.getDataByInstance(1, 1, null,null,obj);
					if (pm.getTotal()>0) {	
						//System.out.println("校验登录间隔");
						obj = (ZsUser)pm.getObjects().get(0);
						if (null==obj.getLoginTime())obj.setLoginTime(dateutil.getCurrentDate5());//如果登录时间为null，默认设置当前时间
						boolean timeactive = true;
						if (Status.USER_ACTIVE_OFF.getStatus().equals(obj.getUsActive())) {
							dataMap.put("message", "activefailed");
						} else if (null!=obj.getLoginTime()) {//首先验证登录间隔
							DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
							Date olddate = format.parse(obj.getLoginTime());
							Date newdate = new Date();
							Calendar c1 = Calendar.getInstance();
					        Calendar c2 = Calendar.getInstance();
					        c1.setTime(olddate);
					        c2.setTime(newdate);
					        System.out.println("现在时间："+newdate +" 上次冻结时间："+ olddate);
					        int timelong = ((c2.get(Calendar.DAY_OF_YEAR) - c1.get(Calendar.DAY_OF_YEAR))*24*60*60 + (c2.get(Calendar.HOUR) - c1.get(Calendar.HOUR))*60*60 +(c2.get(Calendar.MINUTE) - c1.get(Calendar.MINUTE))*60+(c2.get(Calendar.SECOND) - c1.get(Calendar.SECOND)));
					        System.out.println("时间间隔："+timelong+"秒");
					        
					        int count = 5;
					        if(null==obj.getUsYun()) {//如果错误登录次数为空默认设置0
					        	obj.setUsYun("0");
					        }
					        if ("是".equals(anConfigManager.getSingleConfigInfoByName("启用登录错误冻结"))&& Integer.parseInt(obj.getUsYun())>=count && timelong<60) {//距离上次密码或者身份错误小于60秒
					        	System.out.println("冻结中");
					        	dataMap.put("message", "timefailed");
					        	timeactive = false;
					        } 
					        if (timeactive) {//如果间隔时间允许
					        	//System.out.println("@@@@@@@@@@@@@@@"+obj.getUsPassword()+"   "+password+" "+md5.toMD5(password) + " "+Status.USER_ACTIVE_ON.getStatus().equals(obj.getUsActive()));
					        	if (obj.getUsPassword().equals(md5.toMD5(password)) && Status.USER_ACTIVE_ON.getStatus().equals(obj.getUsActive())) {
					        		//System.out.println("登录成功");
					        		obj.setLoginTime(dateutil.getCurrentDate5());
					        		obj.setUsYun("0");
									manager.updateData(obj);
									
									session.setAttribute("zsUser", obj);
									ZsUserStatus status = smanager.getDataByUser(obj);
									session.setAttribute("zsUserStatus", status);
									
									AnLog log = new AnLog(obj.getUsId(),"考生日志",obj.getUsName()+"("+logManager.subIdcard(obj.getUsIdcard())+")"+"于"+dateutil.getCurrentDate5()+" 登录成功", dateutil.getCurrentDate5());
									logManager.addData(log);
									//System.out.println("$$$$$$$$$$$$$$$$$$"+obj.getLoginTime());
									dataMap.put("message", "success");
					        	} else {
					        		System.out.println("密码错误或者被冻结");
					        		if(Integer.valueOf(obj.getUsYun())>=5) {//重置错误次数
					        			obj.setUsYun("0");
					        		}
					        		obj.setLoginTime(dateutil.getCurrentDate5());
					        		obj.setUsYun(String.valueOf(Integer.valueOf(obj.getUsYun())+1));
					        		//System.out.println("登录错误尝试次数："+obj.getUsYun());
									manager.updateData(obj);
					        		dataMap.put("message", "failed");
					        	}
					        }
						}
					} else {
						dataMap.put("message", "failed");//查无此账号
					}				
				}		
			} else {
				dataMap.put("message", "failed");//否则禁止登录
			}
			
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public String zsUserLogin2() {
		System.out.println("-------UserLogin2-------");
		try {
			dataMap = new HashMap<String, Object>();
			String adminJSON = rowstr;
			//System.out.println("rand---"+JSONUtilClass.getMapFromJson(adminJSON).get("rand"));
			System.out.println(adminJSON);			
			
			if(Status.ON.getStatus().equals(((ZsSchool)scmanager.getDatas().get(0)).getScLoginActive())){//如果登录接口开启

				obj = (ZsUser)JSONUtilClass.getDTO(adminJSON, ZsUser.class);
				String password = obj.getUsPassword(); 
				if (!JSONUtilClass.getMapFromJson(adminJSON).get("rand").equals(session.getAttribute("rand"))) {
					dataMap.put("message", "randfailed");
				} else if (null==obj.getUsPassword() || obj.getUsPassword().trim().length()==0) {
					dataMap.put("message", "failed");
				} else {
					obj.setUsPassword(null);
					PageModule pm = manager.getDataByInstance(1, 1, null,null,obj);
					if (pm.getTotal()>0) {	
						//System.out.println("校验登录间隔");
						obj = (ZsUser)pm.getObjects().get(0);
						
						boolean timeactive = true;
						if (null==obj.getLoginTime())obj.setLoginTime(dateutil.getCurrentDate5());//如果登录时间为null，默认设置当前时间
						if (null!=obj.getLoginTime()) {//首先验证登录间隔
							DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
							Date olddate = format.parse(obj.getLoginTime());
							Date newdate = new Date();
							Calendar c1 = Calendar.getInstance();
					        Calendar c2 = Calendar.getInstance();
					        c1.setTime(olddate);
					        c2.setTime(newdate);
					        System.out.println("现在时间："+newdate +" 上次冻结时间："+ olddate);
					        int timelong = ((c2.get(Calendar.DAY_OF_YEAR) - c1.get(Calendar.DAY_OF_YEAR))*24*60*60 + (c2.get(Calendar.HOUR) - c1.get(Calendar.HOUR))*60*60 +(c2.get(Calendar.MINUTE) - c1.get(Calendar.MINUTE))*60+(c2.get(Calendar.SECOND) - c1.get(Calendar.SECOND)));
					        System.out.println("时间间隔："+timelong+"秒");
					        
					        int count = 5;
					        if(null==obj.getUsYun()) {//如果错误登录次数为空默认设置0
					        	obj.setUsYun("0");
					        }
					        if ("是".equals(anConfigManager.getSingleConfigInfoByName("启用登录错误冻结"))&& Integer.parseInt(obj.getUsYun())>=count && timelong<60) {//距离上次密码或者身份错误小于60秒
					        	System.out.println("冻结中");
					        	dataMap.put("message", "timefailed");
					        	timeactive = false;
					        } 
					        if (timeactive) {//如果间隔时间允许
					        	System.out.println("############v "+obj.getUsPassword()+"  "+obj.getUsPassword().equals(md5.toMD5(password)));
					        	if (obj.getUsPassword().equals(md5.toMD5(password))) {
					        		System.out.println("登录成功");
					        		obj.setLoginTime(dateutil.getCurrentDate5());
					        		obj.setUsYun("0");
					        		obj.setUsActive(Status.USER_ACTIVE_ON.getStatus());
									manager.updateData(obj);
									//System.out.println("@@@@@@@@@@@@@@@"+obj.getLoginTime());
									session.setAttribute("zsUser", obj);
									ZsUserStatus status = smanager.getDataByUser(obj);
									session.setAttribute("zsUserStatus", status);
									
									AnLog log = new AnLog(obj.getUsId(),"考生日志",obj.getUsName()+"("+logManager.subIdcard(obj.getUsIdcard())+")"+"于"+dateutil.getCurrentDate5()+" 登录成功", dateutil.getCurrentDate5());
									logManager.addData(log);
									//System.out.println("$$$$$$$$$$$$$$$$$$"+obj.getLoginTime());
									dataMap.put("message", "success");
					        	} else {
					        		System.out.println("密码错误或者被冻结");
					        		if(Integer.valueOf(obj.getUsYun())>=5) {//重置错误次数
					        			obj.setUsYun("0");
					        		}
					        		obj.setLoginTime(dateutil.getCurrentDate5());
					        		obj.setUsYun(String.valueOf(Integer.valueOf(obj.getUsYun())+1));
					        		System.out.println("登录错误尝试次数："+obj.getUsYun());
									manager.updateData(obj);
					        		dataMap.put("message", "failed");
					        	}
					        }
						}
					} else {
						dataMap.put("message", "failed");//查无此账号
					}				
				}		
			} else {
				dataMap.put("message", "failed");//否则禁止登录
			}
			
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	/**
	 * 发送短信
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String sendSMS() {
		try {
			request.setCharacterEncoding("UTF-8");
			dataMap = new HashMap<String, Object>();
			
			String mobile = request.getParameter("usMobile");
			System.out.println("##############"+mobile);
			
			// String smsText = request.getParameter("smsText");
			Random random = new Random();
			String code = random.nextInt(9) + "" + random.nextInt(9) + ""
					+ random.nextInt(9) + "" + random.nextInt(9)
					+ random.nextInt(9) + "" + random.nextInt(9);
			session.setAttribute("code", code);
			try {
				long currentTime = System.currentTimeMillis();
				if(null==session.getAttribute("sms"+mobile)) {
					session.setAttribute("sms"+mobile, currentTime);
					dataMap.put("flag", "success");
					String yzm = anConfigManager.getSingleConfigInfoByName("验证码前缀");
					String kzm = anConfigManager.getSingleConfigInfoByName("扩展码");
					SMSHelper.sendSMS(SMSHelper.generateMobiles(mobile), yzm+"您的验证码是"+ code, kzm); 
					System.out.println("sms"+mobile+"发送验证码:"+code+" 加密后:"+md5.toMD5(code));
					//dataMap.put("code", md5.toMD5(code));// 返回页面当前验证码
					session.setAttribute("sms"+mobile, currentTime);
				} else {
					long sendTime = Long.valueOf(session.getAttribute("sms"+mobile).toString());
					System.out.println("sms"+mobile+"短信发送间隔时间："+(currentTime-sendTime)/1000+"ms");
					if((currentTime-sendTime)/1000>60) {
						dataMap.put("flag", "success");
						String yzm = anConfigManager.getSingleConfigInfoByName("验证码前缀");
						String kzm = anConfigManager.getSingleConfigInfoByName("扩展码");
						SMSHelper.sendSMS(SMSHelper.generateMobiles(mobile), yzm+"您的验证码是"+ code, kzm);
						System.out.println("sms"+mobile+"发送验证码:"+code+" 加密后:"+md5.toMD5(code));
						//dataMap.put("code", md5.toMD5(code));// 返回页面当前验证码
						session.setAttribute("sms"+mobile, currentTime);
					} else {
						dataMap.put("flag", "timeerror");
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				dataMap.put("flag", "error");
			}
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}		
	}

	public String sendPassword() {
		dataMap = new HashMap<String, Object>();
		String objumniJSON = rowstr;
		ZsUser user = (ZsUser)JSONUtilClassTime.getDTO(objumniJSON, ZsUser.class);
		//System.out.print("$$$$$$$"+objumniJSON);
		try {
			if (!JSONUtilClass.getMapFromJson(objumniJSON).get("code").equals(session.getAttribute("code"))) {
				dataMap.put("message", "failed");
			} else if(stringutil.isEmpty(user.getUsIdcard())) {
				dataMap.put("message", "failed");
			} else {	
				ZsApplyView view = new ZsApplyView();
				view.setUsIdcard(user.getUsIdcard());
				view.setUsMobile(user.getUsMobile());
				
				int count = vmanager.getCountByInstance(view);
				if (count<1) {//如果不对，适用导入的InLxdh字段匹配
					view.setUsMobile(null);
					view.setInLxdh(user.getUsMobile());
					count = vmanager.getCountByInstance(view);
					if (count<1) {//如果不对，适用导入的InLxdh2字段匹配
						view.setInLxdh(null);
						view.setInLxdh2(user.getUsMobile());
						count = vmanager.getCountByInstance(view);
					}
				}
				//如果count>0表示该手机号码和身份证可以匹配
				if (count>0) {
					obj = new ZsUser();
					obj.setUsIdcard(user.getUsIdcard());
					PageModule pm = manager.getDataByInstance(1, 1, null,null,obj);
					//System.out.println("@@@@@@@@@@@@@@@@@@@@"+pm.getTotal());
					if (pm.getTotal()>0) {
						obj = (ZsUser)pm.getObjects().get(0);
						obj.setUsPassword(md5.toMD5(user.getUsPassword()));
						obj.setUsMobile(user.getUsMobile());//更新用户注册手机号码
						obj.setUsYun("0");//重置登录错误次数
						if(Status.USER_ACTIVE_OFF.getStatus().equals(obj.getUsActive())) {//如果尚未认证
							obj.setUsActive(Status.USER_ACTIVE_ON.getStatus());//设置为已认证
							obj.setEditTime(dateutil.getCurrentDate5());
							obj.setLoginTime(dateutil.getCurrentDate5());
						}					
						manager.updateData(obj);
						session.setAttribute("user", obj);
						dataMap.put("message", "success");	
						AnLog log = new AnLog(obj.getUsId(),"考生日志",obj.getUsName()+"("+logManager.subIdcard(obj.getUsIdcard())+")"+"于"+dateutil.getCurrentDate5()+" 认证成功", dateutil.getCurrentDate5());
						logManager.addData(log);
					}					
				} else {
					dataMap.put("message", "failed");
				}
			}			
		} catch (Exception e) {
			e.printStackTrace();
			dataMap.put("message", "failed");
		}	
		
		//System.out.println("#####################"+dataMap.get("message"));
		return SUCCESS;
	}

	public String resetPassword() {
		dataMap = new HashMap<String, Object>();
		String objumniJSON = rowstr;
		ZsUser user = (ZsUser)JSONUtilClassTime.getDTO(objumniJSON, ZsUser.class);
		try {
			if (!JSONUtilClass.getMapFromJson(objumniJSON).get("code").equals(session.getAttribute("code"))) {
				dataMap.put("message", "failed");
			} else if(stringutil.isEmpty(user.getUsIdcard()) || stringutil.isEmpty(user.getUsMobile())) {
				dataMap.put("message", "failed");
			} else {				
				obj = new ZsUser();
				obj.setUsIdcard(user.getUsIdcard());
				obj.setUsMobile(user.getUsMobile());
				PageModule pm = manager.getDataByInstance(1, 1, null,null,obj);
				//System.out.println("@@@@@@@@@@@@@@@@@@@@"+pm.getTotal());
				if (pm.getTotal()>0) {
					obj = (ZsUser)pm.getObjects().get(0);
					obj.setUsPassword(md5.toMD5(user.getUsPassword()));
					obj.setUsYun("0");//重置登录错误次数
					if(Status.USER_ACTIVE_OFF.getStatus().equals(obj.getUsActive())) {//如果尚未认证
						obj.setUsActive(Status.USER_ACTIVE_ON.getStatus());//设置为已认证
						obj.setEditTime(dateutil.getCurrentDate5());
						obj.setLoginTime(dateutil.getCurrentDate5());
					}					
					manager.updateData(obj);
					session.setAttribute("user", obj);
					dataMap.put("message", "success");	
				}
			}			
			
		} catch (Exception e) {
			e.printStackTrace();
			dataMap.put("message", "failed");
		}
		return SUCCESS;
	}

	public String checkCode() {//检测手机验证码
		dataMap = new HashMap<String, Object>();
		//String objumniJSON = rowstr;
		try {
			if (!session.getAttribute("code").equals(request.getParameter("code"))) {
				dataMap.put("message", "failed");
			} else {
				dataMap.put("message", "success");			
			}
		} catch (Exception e) {
			e.printStackTrace();
			dataMap.put("message", "failed");
		}
		return SUCCESS;
	}
	
	@Override
	public ZsUser getModel() {
		if (null == obj) {
			obj = new ZsUser();
		}
		return obj;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;
		session = request.getSession();
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		response = arg0;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public ZsUser getAl() {
		return obj;
	}

	public void setAl(ZsUser obj) {
		this.obj = obj;
	}

	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}

	public String getRowstr() {
		return rowstr;
	}

	public void setRowstr(String rowstr) {
		this.rowstr = rowstr;
	}

	public ZsUserManager getManager() {
		return manager;
	}

	public void setManager(ZsUserManager manager) {
		this.manager = manager;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getSearch() {
		return search;
	}

	public void setSearch(String search) {
		this.search = search;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

}
